#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <thread>
#include <sw/redis++/redis++.h>

using namespace std;
using namespace sw::redis;
using namespace std::chrono_literals;

void test1(Redis &redis)
{
    cout << "using sadd and smembers" << endl;

    redis.sadd("key", "111");

    redis.sadd("key", {"222", "333", "444"});

    vector<string> values = {"555", "666", "777"};
    redis.sadd("key", values.begin(), values.end());

    set<string> result;
    auto it = inserter(result, result.end());
    redis.smembers("key", it);
    for (auto &str : result)
        cout << str << endl;
}

void test2(Redis &redis)
{
    cout << "using sismember and scard" << endl;
    redis.sadd("key", {"111", "222", "333"});

    cout << redis.sismember("key", "22") << endl;

    cout << redis.scard("key") << endl;
}

void test3(Redis &redis)
{
    cout << "using sinter and sinterstore" << endl;
    redis.sadd("key1", {"111", "222", "333", "444"});
    redis.sadd("key2", {"555", "666", "333", "444"});

    set<string> values;
    auto it = inserter(values, values.end());
    // redis.sinter({"key1", "key2"}, it);
    // for(auto& str : values)
    //     cout << str << endl;

    long long len = redis.sinterstore("key3", {"key1", "key2"});
    cout << len << endl;

    redis.smembers("key3", it);
    for (auto &str : values)
        cout << str << endl;
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    test3(redis);

    redis.flushall();
    return 0;
}